package comment

import (
	"dy/internal/api/response"
	"dy/pkg/code"
	"dy/pkg/jwt"
	"strconv"

	"github.com/gin-gonic/gin"
)

func (u *ControllerComment) CommentAction(ctx *gin.Context) {
	type param struct {
		Token       string `form:"token" json:"token" binding:"required"`
		VideoId     int    `form:"video_id" json:"video_id" binding:"required"`
		ActionType  int    `form:"action_type" json:"action_type" binding:"required"`
		CommentText string `form:"comment_text" json:"comment_text" binding:"-"`
		CommentId   int    `form:"comment_id" json:"comment_id" binding:"-"`
	}
	var request param
	err := ctx.Bind(&request)
	if err != nil {
		code.ErrReturn(ctx, err.Error())
		return
	}

	userId, err := jwt.ParseToken(request.Token)
	if err != nil {
		code.ErrReturn(ctx, err.Error())
		return
	}

	search := map[string]interface{}{}
	search["user_id"] = userId
	search["video_id"] = request.VideoId
	search["action_type"] = request.ActionType
	search["content"] = request.CommentText
	// search["del_time"] = "2006-01-02 15:04:05"
	search["id"] = request.CommentId

	commentId, err := u.srv.Comment().Action(ctx, search)
	if err != nil {
		code.ErrReturn(ctx, err.Error())
		return
	}

	user, flag, err := u.store.User().GetById(ctx, strconv.Itoa(search["user_id"].(int)))
	if err != nil || !flag {
		return
	}
	userResponse := response.UserResponse{
		Id:              user.Id,
		Name:            user.Name,
		FollowCount:     10,
		FollowerCount:   10,
		IsFollow:        true,
		Avatar:          "",
		BackgroundImage: "",
		Signature:       "",
		TotalFavorited:  100,
		WorkCount:       100,
		FavoriteCount:   100,
	}

	comment, flag, err := u.store.Comment().GetById(ctx, commentId)
	if err != nil || !flag {
		return
	}

	createTime, _ := comment.AddTime.MarshalJSON()
	commentResponse := response.CommentResponse{
		Id:         comment.Id,
		User:       userResponse,
		Content:    comment.Content,
		CreateData: string(createTime),
	}

	ctx.JSON(200, gin.H{
		"status_code": 0,
		"status_msg":  "success",
		"comment":     commentResponse,
	})
}
